package org.dromara.authcode.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.authcode.domain.vo.TDeviceLocationVo;
import org.dromara.authcode.domain.bo.TDeviceLocationBo;
import org.dromara.authcode.service.ITDeviceLocationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 设备位置记录
 *
 * @author Miles Meng
 * @date 2025-08-11
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/authcode/deviceLocation")
public class TDeviceLocationController extends BaseController {
    private final ITDeviceLocationService tDeviceLocationService;

    /**
     * 查询设备位置记录列表
     */
    @SaCheckPermission("authcode:deviceLocation:list")
    @GetMapping("/list")
    public TableDataInfo<TDeviceLocationVo> list(TDeviceLocationBo bo, PageQuery pageQuery) {
        return tDeviceLocationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设备位置记录列表
     */
    @SaCheckPermission("authcode:deviceLocation:export")
    @Log(title = "设备位置记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TDeviceLocationBo bo, HttpServletResponse response) {
        List<TDeviceLocationVo> list = tDeviceLocationService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备位置记录", TDeviceLocationVo.class, response);
    }

    /**
     * 获取设备位置记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("authcode:deviceLocation:query")
    @GetMapping("/{id}")
    public R<TDeviceLocationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tDeviceLocationService.queryById(id));
    }

    /**
     * 新增设备位置记录
     */
    @SaCheckPermission("authcode:deviceLocation:add")
    @Log(title = "设备位置记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TDeviceLocationBo bo) {
        return toAjax(tDeviceLocationService.insertByBo(bo));
    }

    /**
     * 修改设备位置记录
     */
    @SaCheckPermission("authcode:deviceLocation:edit")
    @Log(title = "设备位置记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TDeviceLocationBo bo) {
        return toAjax(tDeviceLocationService.updateByBo(bo));
    }

    /**
     * 删除设备位置记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("authcode:deviceLocation:remove")
    @Log(title = "设备位置记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tDeviceLocationService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取设备位置记录
     *
     * @param macAddress mac地址
     * @return 设备位置记录
     */
//    @SaCheckPermission("authcode:deviceLocation:query")
    @SaIgnore
    @GetMapping("/getLastLocationByMac")
    public R<TDeviceLocationVo> getLastLocationByMac(@RequestParam("macAddress") String macAddress) {
        return R.ok(tDeviceLocationService.getLastLocationByMac(macAddress));
    }

    /**
     * 记录设备位置
     *
     * @param bo 设备位置记录
     */
//    @SaCheckPermission("authcode:deviceLocation:add")
    @SaIgnore
    @PostMapping("/record")
    public R<Void> record(@RequestBody TDeviceLocationBo bo) {
        log.info("记录设备位置:{}", bo);
        return toAjax(tDeviceLocationService.recordDeviceLocation(bo));
    }

}
